package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.print.attribute.HashPrintJobAttributeSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author hsp
 * @date 2021/5/24 11:07
 */
@Service
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;
    @Override
    public HomePageResponse homepage() {

        List<Panel> panels = panelMapper.selectAll();
        for (Panel panel : panels) {
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            panel.setPanelContentItems(panelContentItems);
        }
        Set<PanelDto> panelContentItemDtos = new HashSet<>();
        for (Panel panel : panels) {
        PanelDto panelDto = contentConverter.panen2Dto(panel);
            panelContentItemDtos.add(panelDto);
        }


        HomePageResponse homePageResponse = new HomePageResponse();
        homePageResponse.setPanelContentItemDtos(panelContentItemDtos);
        homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        return homePageResponse;
    }
}
